//
//  OALoginViewController.m
//  OASystem
//  
//  Created by zhoupengfei on 15/3/23.
//  Copyright (c) 2015年 zpf. All rights reserved.
//

#import "OALoginViewController.h"
#import "Toast+UIView.h"
#import "HttpTools.h"
#import "MBProgressHUD.h"
#import "XmlParseHelper.h"
#import "AnjiMainViewController_Second.h"

@interface OALoginViewController ()<UITextFieldDelegate>


@end

@implementation OALoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //隐藏状态栏
    [[UIApplication sharedApplication] setStatusBarHidden:TRUE];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] length] !=0) {
        self.accountTextField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    }
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"pwdId"] length] !=0) {
        self.passwordTextField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"pwdId"];
    }
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"autoLogin"] isEqualToString:@"Y"]) {
        self.autoLogin.selected = YES;
    }
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [self.accountTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
}

//记住账号
- (IBAction)recordAction:(id)sender {
    
    self.recordPassWord.selected = !self.recordPassWord.selected;
    if (!self.recordPassWord.selected) {
        self.autoLogin.selected = self.recordPassWord.selected;
    }
    
} 


//记住密码
- (IBAction)autoLogin:(id)sender {
    self.autoLogin.selected = !self.autoLogin.selected;
}

- (IBAction)LoginAction:(UIButton *)sender {
    [self.accountTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    
    if (self.accountTextField.text.length == 0) {
        [self.view makeToast:@"请输入账号"];
        return;
    }
    if (self.passwordTextField.text.length == 0) {
        [self.view makeToast:@"请输入密码"];
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params setObject:self.accountTextField.text forKey:@"userId"];
    [params setObject:self.passwordTextField.text forKey:@"pwdId"];

    [HttpTools postWithURL:KAnJiLoginURL arrays:@[@{@"userId":self.accountTextField.text},@{@"pwdId":self.passwordTextField.text},@{@"oaID":@"1"}] method:@"validationUser" success:^(id JSON) {
       [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        XmlParseHelper * parse = [[XmlParseHelper alloc]initWithData:JSON];
        
        NSArray * dataArray = [parse selectNodes:@"errmsg"];
        NSString * resultTag = nil;
        if (dataArray.count) {
            NSDictionary * dic  = [dataArray lastObject];
            resultTag = dic[@"text"];
        }
         AnjiMainViewController_Second * mainVC = [self.storyboard instantiateViewControllerWithIdentifier:@"AnjiMainVC"];
        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
        switch ([resultTag integerValue]) {
            case 1:
                 [self.view makeToast:@"您的用户名不存在，请确认后再输"];
                break;
                case 2:
                self.view.window.rootViewController = navVC;

                [[NSUserDefaults standardUserDefaults] setObject:self.accountTextField.text forKey:@"userId"];
                [[NSUserDefaults standardUserDefaults] setObject:self.passwordTextField.text forKey:@"pwdId"];
                //自动登录的
                if (!self.autoLogin.selected) {
                    [[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"autoLogin"];
                }else{
                    [[NSUserDefaults standardUserDefaults] setObject:@"Y" forKey:@"autoLogin"];
                }
             
                break;
                case 3:
                [self.view makeToast:@"您的密码错误，请确认后再输"];
                break;
                case -1099:
                [self.view makeToast:@"抱歉，服务器出错"];
                break;
            default:
                break;
        }
        
    } failure:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];

}




@end
